package Options::Attributes::API::WebServiceLink;
use strict;
use SOAP::Lite+trace => 'debug';
use Data::Dumper;
use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);

sub new
{
    my $class = $_[0];
    my $self;
    my %Author = ('Username'	=> 'XXXXtest',
	    	  'Password'	=> 'xXXXXXU2',
		 );
    $self->{SOAPActionName} = $_[1]; #CustomerSearch
    $self->{SOAPMethodName} = $_[2]; #CustomerSearchRequest    
    $self->{_WSRequest} = $_[3];
    $self->{_ReturnPart} = $_[4];
    $self->{_author} = \%Author;

    $self->{_soap} = SOAP::Lite
	    -> uri('')
	    -> on_action( sub { join '/', 'http://XXXXXXXXXXX-mailorder.co.uk', $self->{SOAPActionName}})
	    -> proxy('http://XXXXXXXXXXXks.co.uk/owsweb/owsweb.asmx');
    bless $self, $class;
    return $self;
}

sub _setupParams {
#This method should be overriden by Interface.
			my $self = shift;
			GetLog->debug("HTTP Version");
			my @params = (
			    SOAP::Data->type('stringx')->name('ShopperID' => $self->{_WSRequest}->{ShopperID}),
			    SOAP::Data->type('boolean')->name('GetAllCustomers' => $self->{_WSRequest}->{GetAllCustomers})
			);
			return @params;
}

sub _setupMethod {
			my $self = shift;
			my $method = SOAP::Data->name($self->{SOAPMethodName})
    					       ->attr({xmlns => 'http://XXXXXXXXXXX-mailorder.co.uk/'});
			return $method;	
}

sub _setupHeaders {
				my $self = shift;
				my $Author = SOAP::Header->name('Author')
            ->attr({'xmlns' => 'http://XXXXXXXXXXX-mailorder.co.uk/'})
            ->value(\SOAP::Header->value(SOAP::Header->name('UserID' => $self->{_author}->{Username}),
                                         SOAP::Header->name('Password' => $self->{_author}->{Password})
				));
				my @headers = ($Author);
    		return @headers;	
}

sub ChangeRequestParameters {
				my $self = shift;
				my $NewPrices = shift;
				foreach my $cfg (keys %$NewPrices){
					$self->{_WSRequest}->{$cfg} = $NewPrices->{$cfg};
				}
}

sub hitMe {
				my $self = shift;
				my $respond = $self->{_soap}->call($self->_setupMethod() => $self->_setupParams(), $self->_setupHeaders())->result;		
				if (ref($respond) eq "HASH") {
				    if ($self->{_ReturnPart} ne "") {
					return $respond->{$self->{_ReturnPart}};
				    } else {
					return $respond;
				    }
			  	} else {
			  		return $respond;
				}
}

1;
